const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('/')
}
/**
 * 用于判断空，Undefined String Array Object
 */
function isBlank(str) {
  if (Object.prototype.toString.call(str) === '[object Undefined]') {//空
    return true
  } else if (
    Object.prototype.toString.call(str) === '[object String]' ||
    Object.prototype.toString.call(str) === '[object Array]') { //字条串或数组
    return str.length == 0 ? true : false
  } else if (Object.prototype.toString.call(str) === '[object Object]') {
    return JSON.stringify(str) == '{}' ? true : false
  } else {
    return true
  }

}
function showOkModel(str) {
  wx.showModal({
    showCancel: false,
    title: '提示',
    content: str,
  })

}
function showOkToast(str) {
  wx.showToast({
    title: str,
    icon: '',
    image: '/images/submit.png',
    duration: 2000,
    mask: true,
    success: function (res) { },
    fail: function (res) { },
    complete: function (res) { },
  })
}
function showErrorToast(str) {
  wx.showToast({
    title: str,
    icon: '',
    image: '/images/error-1.png',
    duration: 2000,
    mask: true,
    success: function (res) { },
    fail: function (res) { },
    complete: function (res) { },
  })
}

//判断是否为中文
function IsChinese(str) {
  var reg = /^[\u0391-\uFFE5]+$/;
  if (reg.test(str)) {
    return true;
  } else {
    return false;
  }
}
/*
*电话校验
*/
function checkPhoneNum(phoneNumber) {
  let str = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/
  if (str.test(phoneNumber)) {
    return true;
  } else {
    return false;
  }
}
//校验邮箱
function checkEmail(email) {
  let str = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/
  if (str.test(email)) {
    return true
  } else {
    wx.showToast({
      title: '请填写正确的邮箱',
      duration: 2000,
    })
    return false
  }
}
//显示距离
function showDistance(distance) {
  var intdistance = parseInt(distance);
  if (intdistance < 1000 && 0 <= intdistance) {
    return intdistance + '米';
  } else {
    return (intdistance / 1000).toFixed(2) + '千米';
  }
}
//判断是否为数字
function isNumber(numberstr) {
  var regNum = new RegExp('[0-9]', 'g');//判断用户输入的是否为数字
  var isnumber = regNum.exec(numberstr);
  if (isnumber) {
    return true;
  } else {
    return false;
  }
}
module.exports = {
  formatTime: formatTime,
   isBlank: isBlank,
  showOkModel: showOkModel,
  showOkToast: showOkToast,
  IsChinese: IsChinese,
  checkPhoneNum: checkPhoneNum,
  checkEmail: checkEmail,
  formatDate: formatDate,
  showDistance: showDistance,
  isNumber: isNumber,
  showErrorToast: showErrorToast

}

